<?php
class ControllerSAdminBooking extends Controller {
	public $m_venue_id = null;
	public $m_page        = 1;
	
	public $m_control_plan   = null;
	public $m_control_venue = null;
	public $m_control_space =  null;
	public $m_control_event =  null;
	public $m_control_book   = null;
	
	
	public $m_space_name_list = array();
	public $m_event_name_list = array();
	public $m_time_list              = array();
	
	public $m_base_url  = '';
	public $m_limit  = 10;
	
	public function __construct($registry)
	{
		$this->registry = $registry;
		
		$this->document->setTitle('运动场馆管理系统 - ' . CONF_TITLE);
		
		$this->template = '/template/s_admin/booking.html';
		
		$this->children = array(
				'common/footer',
				's_admin/model/header',
				's_admin/model/menu'
		);
		
		$this->document->addScript('/theme/javascript/jquery.ui.datepicker-zh-CN.js');
		// 场馆ID
		if (isset($this->request->get ['id']))
		{
			$this->m_venue_id = $this->request->get ['id'];
		}
		
		if (isset($this->request->get['page'])) {
			$this->m_page = (int)$this->request->get['page'] > 0 ? (int)$this->request->get['page'] : 1;
		} else {
			$this->m_page = 1;
		}
		
		//子菜单
		$this->document->addSubmenu('个人中心',   $this->url->link('s_admin/info'));
		$this->document->addSubmenu('场馆标签',   $this->url->link('s_admin/label'));
		$this->document->addSubmenu('场馆评分',   $this->url->link('s_admin/score'));
		$this->document->addSubmenu('场馆图片',   $this->url->link('s_admin/picture'));
		$this->document->addSubmenu('场馆资讯',   $this->url->link('s_admin/news'));
		$this->document->addSubmenu('场地设置',   $this->url->link('s_admin/setting'));
		$this->document->addSubmenu('日常管理',   $this->url->link('s_admin/daily'), 'class="hover"');
		$this->document->addSubmenu('订单管理',   $this->url->link('s_admin/order'));
		$this->document->addSubmenu('报表管理',   $this->url->link('s_admin/statements'));
		$this->document->addSubmenu('操作员管理', $this->url->link('s_admin/operate'));
		
		// model 载入
		$this->load->model('sport/plan');
		$this->m_control_plan               = $this->model_sport_plan;
		$this->load->model('sport/venue');
		$this->m_control_venue            = $this->model_sport_venue;
		$this->load->model('sport/event');
		$this->m_control_event            = $this->model_sport_event;
		$this->load->model('sport/space');
		$this->m_control_space            = $this->model_sport_space;
		$this->load->model('sport/book');
		$this->m_control_book            = $this->model_sport_book;
		
		$this->m_base_url =  'index.php?route=s_admin/booking&id='.$this->m_venue_id;
		
	}
	public function index()
	 {
	
	 	// 场地 名列表
		$space_list = $this->m_control_space->getSpaceList(array('venueId' => $this->m_venue_id,'status'=> 1));
		foreach ($space_list as $space)
		{
			$this->m_space_name_list[$space['id']] = $space['name'];
		}
		
	 	// 项目名列表
		$event_list = $this->m_control_event->getEventName();

		foreach ($event_list as $event)
		{
			$this->m_event_name_list[$event['ID']] = $event['Name'];
		}
		
		// 获取周期信息
		
		$this->data['book_list'] = array();
		$total = count($this->m_control_book->getBookList(array('venueID' => $this->m_venue_id,'noStatus' => 2)));
		$book_list = $this->m_control_book->getBookList(array('venueID' => $this->m_venue_id,'noStatus' => 2),($this->m_page - 1)*$this->m_limit,$this->m_limit);
		
		foreach ($book_list as $book)
		{
			if ($book['cycle'] == 0)
			{
				$cycle = $book['cycleValue'].'号';
			}
			elseif($book['cycle'] == 1)
			{
				$cycle = '周'.$book['cycleValue'];
			}
			$this->data['book_list'][] = array(
					'id' => $book['id'],
					'userName' => $book['userName'],
					'tel' => $book['tel'],
					'date' => $book['startDate'].' 到 '.$book['endDate'],
					'time'=>  substr($book['startTime'], 0,5).'-'.substr($book['endTime'], 0,5),
					'cycle'=> $cycle,
					'cycle_id' => $book['cycle'],
					'cycleValue'=> $book['cycleValue'],
					'spaceName' => $this->m_space_name_list[$book['spaceID']],
					'spaceID' => $book['spaceID'],
					'eventID'=>  $book['eventID'],
					'eventName'=> $this->m_event_name_list[$book['eventID']],
					'price'=> $book['price']/100,
					'status'=> $book['status'] == 0 ?'否':"是",
					'startDate' => $book['startDate'],
					'endDate' => $book['endDate'],
					'remark'  => $book['remark'],

					);
		}
		
		
		// 获取场馆时间段
		
		$tpl_list = $this->m_control_plan->getTemplateList(array('venueId' => $this->m_venue_id,'week' => 1));
		
		foreach ($tpl_list as $value)
		{
			$timeKey = substr($value['startTime'], 0,5).'-'.substr($value['endTime'], 0,5);
// 			$this->m_time_list[$value['spaceId']][$timeKey] = 	$timeKey;
           // TODO: 场馆的每个场地的时间段在此处设定为默认为一样  2013-01-05  LSH
			$this->m_time_list[$timeKey] = 	$timeKey;
			
		}
		
		$this->data['space_time_list']  = $this->m_time_list;
		$this->data['space_name_list'] = $this->m_space_name_list;
		$this->data['event_name_list'] = $this->m_event_name_list;
		
		
		$this->data['ajaxUrl'] = array(
				'update' => 'index.php?route=s_admin/booking/update&id='.$this->m_venue_id,
				'add'      => 'index.php?route=s_admin/booking/add&id='.$this->m_venue_id,
				'del'       => 'index.php?route=s_admin/booking/del&id='.$this->m_venue_id,
				'index'   => 'index.php?route=s_admin/booking/index&id='.$this->m_venue_id,
				'daily'    => 'index.php?route=s_admin/daily/index&id='.$this->m_venue_id,
				'time'    => 'index.php?route=s_admin/booking/time&id='.$this->m_venue_id,
				);
		
		$this->data['venueID'] = $this->m_venue_id;
		
		//分页生成
		$pagination = new Pagination();
		$pagination->total = $total;
		$pagination->page = $this->m_page;
		$pagination->limit = $this->m_limit;
		$pagination->text = '';
		$pagination->url = $this->m_base_url . '&page={page}';
		
		//分页递交
		$this->data['pagination'] = $pagination->render();
		
	
		$this->response->setOutput($this->render());
	}
	
	public function update()
	{
		$post = $this->request->post;
	
	   $post['startTime'] = substr($post['time'], 0,5).':00';
		$post['endTime']  = substr($post['time'], strlen($post['time']) - 5,5).':00';
		
		$post['price']        = intval($post['price']*100);

		if (!isset($post['id']))
		{
			return false;
		}
		
		if ($this->m_control_book->updateBook($post['id'],$post))
		{
			$this->result->status = 1;
		}
		else
		{
			$this->result->status = 0;
		}
		
		$this->response->setOutput($this->result->json_get());
	}
	
	public function add()
	{
		$post = $this->request->post;
	
		
		$post['startTime'] = substr($post['time'], 0,5).':00';
		$post['endTime']  = substr($post['time'], strlen($post['time']) - 5,5).':00';
		$post['price']        = intval($post['price']*100);
		
		$post['venueID'] = $this->m_venue_id;
		
		if ($this->m_control_book->addBook($post))
		{
			$this->result->status = 1;
		}
		else
		{
			$this->result->status = 0;
		}
		
		$this->response->setOutput($this->result->json_get());
	}
	
	public function del()
	{
		$post = $this->request->post;
	
		if ($this->m_control_book->updateBook($post['id'],array('status' => 2)))
		{
			$this->result->status = 1;
		}
		else
		{
			$this->result->status = 0;
		}
		
		$this->response->setOutput($this->result->json_get());
	}
	
	public function time()
	{
		$post = $this->request->post;
		
		if ($post['cycle'] == 1)
		{
			// 获取场馆时间段
			
			$tpl_list = $this->m_control_plan->getTemplateList(array('venueId' => $this->m_venue_id,'week' => $post['cycleValue']));
			
			foreach ($tpl_list as $value)
			{
				$timeKey = substr($value['startTime'], 0,5).'-'.substr($value['endTime'], 0,5);
				$this->m_time_list[$timeKey] = 	$timeKey;
					
			}
		}
		else
		{
			$tpl_list = $this->m_control_plan->getTemplateList(array('venueId' => $this->m_venue_id,));
				
			foreach ($tpl_list as $value)
			{
				$timeKey = substr($value['startTime'], 0,5).'-'.substr($value['endTime'], 0,5);
				$this->m_time_list[$timeKey] = 	$timeKey;
					
			}
		}
		
		$this->result->data = $this->m_time_list;
		$this->response->setOutput($this->result->json_get());
	}
}
?>
